package com.zhongfeng.api.modules.customer.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongfeng.api.modules.customer.mapper.CustomerMapper;
import com.zhongfeng.api.modules.customer.service.CustomerService;
import com.zhongfeng.api.modules.customer.vo.CustomerVo;
import com.zhongfeng.api.modules.customer.vo.params.CustomerParams;
import com.zhongfeng.api.modules.utils.pojo.PageDataInfo;
import com.zhongfeng.api.modules.utils.pojo.Result;
import com.zhongfeng.api.modules.utils.service.HttpStatus;
import com.zhongfeng.api.modules.utils.service.ReturnCodes;
import com.zhongfeng.api.modules.utils.service.ReturnMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author blueCat
 * @date 2023/2/8 10:18
 * @motto life is a gift
 */
@Service
@Slf4j
public class CustomerImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询全部顾客信息
     *
     * @param params
     * @return
     */
    @Override
    public Result queryAllCustomer(CustomerParams params) {
        Page<CustomerVo> page = new Page<>(params.getPageNum(), params.getPageSize());
        List<CustomerVo> customerVos = customerMapper.queryAllCustomer(page, params);
        return Result.setSuccess(HttpStatus.SUCCESS, "", new PageDataInfo(page, customerVos));
    }

    /**
     * 新增客户信息
     *
     * @param params
     * @return
     */
    @Override
    public Result addCustomer(CustomerParams params) {
        int j = customerMapper.checkCompany(params);
        if (j == 0) {
            int i = customerMapper.addCustomer(params);
            if (i == 1) {
                return Result.setSuccess(HttpStatus.SUCCESS, "添加客户成功！");
            } else {
                return Result.setFail(ReturnCodes.ADD_CUSTOMER_FAIL, ReturnMessage.ADD_CUSTOMER_FAIL);
            }
        } else {
            return Result.setFail("该公司名已存在");
        }
    }

    /**
     * 删除客户信息
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteCustomer(Integer id) {
        int i = customerMapper.deleteCustomer(id);
        if (i == 1) {
            return Result.setSuccess(HttpStatus.SUCCESS, "删除客户成功！");
        } else {
            return Result.setFail(ReturnCodes.DELETE_CUSTOMER_FAIL, ReturnMessage.DELETE_CUSTOMER_FAIL);
        }
    }

    /**
     * 更新客户信息
     *
     * @param params
     * @return
     */
    @Override
    public Result updateCustomer(CustomerParams params) {
        int i = customerMapper.updateCustomer(params);
        if (i == 1) {
            return Result.setSuccess(HttpStatus.SUCCESS, "修改客户成功！");
        } else {
            return Result.setFail(ReturnCodes.UPDATE_CUSTOMER_FAIL, ReturnMessage.UPDATE_CUSTOMER_FAIL);
        }
    }
}
